var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone:'',  //手机号
    code: '',  //验证码
    time:30,  //倒计时
    numshow:true,  //'获取验证码'
    setInter: '',  //定时器
    phone1:''
  },
  // 输入手机号
  setvaluephone: function (event) {
    this.setData({
      phone1: event.detail.value
    });
  },
  // 输入验证码
  setvaluecode: function (event) {
    this.setData({
      code: event.detail.value
    });
  },
  // 获取验证码
  getcode: function (event) {
    var that = this;
    var times=that.times
    if(that.data.phone1!=''){
      wx.request({
        url: app.globalData.url + 'api/Client/AjaxVerificationCode',
        method: "POST",
        data: {
          phone: that.data.phone1
        },
        success: function (res) {
          wx.showToast({
            title: res.data.msg
          }, 2000);
        }
      });
      that.setData({
        numshow: false
      });
      that.data.setInter=setInterval(times,1000);
    }   
  },
  // 更新手机号
  okTap: function () {
    if (this.data.phone1.length > 0) {
      var that = this;
      wx.request({
        url: app.globalData.url + 'api/Client/setPhone',
        method: "POST",
        data: {
          openid: app.globalData.openid,
          phone: that.data.phone1,
          code: that.data.code
        },
        success: function (res) {
          wx.showToast({
            title: res.data.msg
          }, 2000);
          if (res.data.status == 1) {
            app.globalData.loginInfo.phone = that.data.phone;
            wx.navigateBack({
              delta:1
            });
          }
        }
      });
    }
  },
  // 倒计时
  times:function(){
    var time=this.data.time;
    time--;
    if (time==0){  
      this.setData({
        numshow: true,
        time: 10
      });
      clearInterval(this.data.setInter);
      return;
    }
    this.setData({
      time: time
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      phone: options.phone
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    //清除计时器  即清除setInter
    clearInterval(that.data.setInter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})